package com.huidu.bitrade.controller;

import cn.hutool.core.lang.Assert;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.huidu.bitrade.config.CustomerBigDecimalSerialize;
import com.huidu.bitrade.constant.MemberLevelEnum;
import com.huidu.bitrade.constant.SysConstant;
import com.huidu.bitrade.entity.Member;
import com.huidu.bitrade.entity.OtcBusinessRealization;
import com.huidu.bitrade.entity.transform.AuthMember;
import com.huidu.bitrade.exception.ServiceException;
import com.huidu.bitrade.service.MemberService;
import com.huidu.bitrade.service.OtcBusinessRealizationService;
import com.huidu.bitrade.util.MessageResult;
import com.huidu.bitrade.vo.BusinessQuotaVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.util.Objects;

@Api(tags = "uc模块/otc商家变现")
@RestController
@RequestMapping("/business-realization")
@Slf4j
@RequiredArgsConstructor
public class OtcBusinessRealizationController {

    private final OtcBusinessRealizationService otcBusinessRealizationService;
    private final MemberService memberService;
    /**
     * otc承兑商变现
     * @return
     */
    @ApiOperation("otc承兑商变现")
    @PostMapping("realization")
    public MessageResult businessRealization(@SessionAttribute(SysConstant.SESSION_MEMBER) AuthMember user,
                                             @RequestParam("amount") BigDecimal amount,
                                             @RequestParam("payType") Integer payType) throws ServiceException {
        Member member = memberService.findOne(user.getId());
        Assert.isTrue(Objects.nonNull(member),"未找到当前用户");
        //判断是否是新承兑商
        if(Objects.isNull(member.getBusinessDepositStrategy())||!member.getMemberLevel().equals(MemberLevelEnum.IDENTIFICATION)){
            return MessageResult.error("您当前不是全员承兑商");
        }
        otcBusinessRealizationService.apply(member,amount,payType);
        return MessageResult.success();
    }

    /**
     * 获取用户可变现额度
     * @return
     */
    @ApiOperation("获取用户可变现额度")
    @GetMapping("quota")
    public MessageResult<BusinessQuotaVo> quota(@SessionAttribute(SysConstant.SESSION_MEMBER) AuthMember user) {
        return MessageResult.success("success",otcBusinessRealizationService.getBusinessQuota(user.getId()));
    }

    /**
     * 查询承兑记录分页
     * @return
     */
    @GetMapping("list")
    @ApiOperation("查询承兑记录分页")
    public MessageResult<Page<OtcBusinessRealization>> list(@ApiIgnore @SessionAttribute(SysConstant.SESSION_MEMBER) AuthMember member,
                                                            @RequestParam(value = "pageNo")int pageNo,
                                                            @RequestParam(value = "pageSize")int pageSize,
                                                            @RequestParam(value = "status",required = false)Integer status,
                                                            @RequestParam(value = "startTime", required = false) String startTime,
                                                            @RequestParam(value = "endTime", required = false) String endTime) throws ParseException {
        return MessageResult.success("SUCCESS",otcBusinessRealizationService.queryByMember(member.getId(), pageNo,pageSize, startTime,endTime,status));
    }

    /**
     * 查询用户已承兑数量
     * @return
     */
    @ApiOperation("查询用户已承兑数量")
    @GetMapping("hisAmount")
    public MessageResult<HisAmount> hisAmount(@SessionAttribute(SysConstant.SESSION_MEMBER) AuthMember user) {
        BigDecimal amount = otcBusinessRealizationService.getAllGain(user.getId());
        return MessageResult.success("success",HisAmount.builder()
                .amount(amount.setScale(2, RoundingMode.DOWN))
                .price(amount.multiply(new BigDecimal("0.985")).setScale(2, RoundingMode.DOWN))
                .build());
    }

    @Data
    @Builder
    public static class HisAmount{
        @ApiModelProperty("承兑数量")
        @JsonSerialize(using = CustomerBigDecimalSerialize.class)
        private BigDecimal amount;

        @ApiModelProperty("cny金额")
        @JsonSerialize(using = CustomerBigDecimalSerialize.class)
        private BigDecimal price;
    }

}
